package com.news.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.news.common.CommonEnum;
import com.news.common.ResponseBean;
import com.news.common.ResultUtil;
import com.news.common.ToolsUtils;
import com.news.entity.NewsFavor;
import com.news.entity.SysNews;
import com.news.entity.SysUser;
import com.news.service.NewsFavorService;
import com.news.service.SysNewsService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户点赞表 前端控制器
 * </p>
 *
 */
@Controller
@RequestMapping({"/newsFavor","/home/newsFavor"})
public class NewsFavorController {

    private static  final Logger logger= LoggerFactory.getLogger(NewsFavorController.class);

    private static final String page_prefix="news/favor/";
    @Autowired
    NewsFavorService favorService;

    @Autowired
    SysNewsService newsService;

    //前端页面进行点赞和取赞 action 1 关注；2 取关
    @RequestMapping("updateFavor")
    @ResponseBody
    public ResponseBean updateFavor(NewsFavor favor, Long action, HttpSession session){
        SysUser loginUser = ToolsUtils.getLoginUser(session);
        Long newsId = favor.getNewsId();
        SysNews news = newsService.getById(newsId);
        if (action==1){
            //进行点赞
            favor.setNewsTitle(news.getTitle());
            favor.setUserId(loginUser.getId());
            favor.setUserName(loginUser.getName());
            favorService.save(favor);
        }else if(action==2){
            QueryWrapper<NewsFavor> wrapper=new QueryWrapper();
            wrapper.eq("news_id",newsId).eq("user_id",loginUser.getId());
            favorService.remove(wrapper);
        }
        return  ResultUtil.success();
    }

    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("/listPage")
    public String listPage() {
        return page_prefix+"list";
    }

    /**
     * 新闻点赞列表查询
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param queryParam 查询参数
     * @param vagueParam 模糊参数
     * @return
     */
    @ApiOperation("获取新闻点赞列表")
    @RequestMapping("/list/{pageNum}/{pageSize}")
    @ResponseBody
    public ResponseBean getList(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                NewsFavor queryParam, String vagueParam) {
        logger.debug("查询新闻点赞列表参数："+queryParam+",pageNum:"+pageNum+",pageSize:"+pageSize);
        QueryWrapper<NewsFavor> queryWrapper=new QueryWrapper<>();
        String newsTitle = queryParam.getNewsTitle();
        String userName = queryParam.getUserName();
        queryWrapper.like(!StringUtils.isEmpty(newsTitle),"news_title",newsTitle)
                .like(!StringUtils.isEmpty(userName),"user_name",userName);
        IPage<NewsFavor> indexPage = new Page<>(pageNum, pageSize);
        IPage<NewsFavor> listPage = favorService.page(indexPage, queryWrapper);
        logger.debug("获取的新闻点赞列表："+listPage);
        //调用工具类对结果进行封装然后返回
        Map resultMap = ToolsUtils.wrapperResult(listPage, "favorList");
        return ResultUtil.successData(resultMap);
    }

    /**
     * 批量删除点赞
     * @param idList
     * @return
     */
    @RequestMapping("deleteBatchByIds")
    @ResponseBody
    public ResponseBean deleteBatchByIds(@RequestParam List<Long> idList){
        logger.debug("deleteBatchByIds:"+idList);
        if(idList==null||idList.isEmpty()){
            return ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        boolean delete = favorService.removeByIds(idList);
        logger.debug("批量删除结果："+delete);
        return ResultUtil.success() ;
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public ResponseBean deleteById(@RequestParam Long id){
        if(id==null||id<0){
            return ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        boolean delete = favorService.removeById(id);
        return ResultUtil.success() ;
    }


}
